package com.engine.jucailinkq.attendance.component.AttendanceCycle.job;

import com.engine.jucailinkq.common.util.DbTools;
import com.time.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: sy
 * @Description: 考勤周期关账
 * @Date: 2024/8/2
 **/
@Slf4j
public class AttendanceCycleCloseJob extends BaseCronJob {
    @Override
    public void execute() {
        log.info("********AttendanceCycleCloseJob  start********");
        //查询当前时间大于等于关账日期+时间的考勤周期
        String currentTime = DateUtil.getCurrentTime("yyyy-MM-dd HH:mm");
        //处理考勤周期主表
        String sql = "select id, gzrq, gzsj, zt from uf_jcl_kq_kqzq where zt = 0";
        List<Map<String, Object>> noCloseMainData = DbTools.getSqlToList(sql);
        noCloseMainData = noCloseMainData.stream().filter(f -> (Util.null2String(f.get("gzrq")) + " " + Util.null2String(f.get("gzsj"))).compareTo(currentTime) <= 0).collect(Collectors.toList());
        if (noCloseMainData.size() > 0) {
            List<String> ids = noCloseMainData.stream().map(e->e.get("id").toString()).collect(Collectors.toList());
            //更新考勤周期的状态为已关账
            sql = "update uf_jcl_kq_kqzq set zt = 1 where id in (" + String.join(",", ids) + ")";
            boolean updateSign = DbTools.update(sql);
            log.info("AttendanceCycleCloseJob-考勤周期主表更新到已关账状态结果：" + updateSign);
        }
        //收集主表id, 以考勤周期名称分组，各组年月最新的一条已关账数据外，其余置为已结账
        sql = "select id, gzrq, gzsj, zt, mc, nd, yf from uf_jcl_kq_kqzq where zt = 1";
        List<Map<String, Object>> closeMainData = DbTools.getSqlToList(sql).stream()
                .filter(f -> (Util.null2String(f.get("gzrq")) + " " + Util.null2String(f.get("gzsj"))).compareTo(currentTime) <= 0).collect(Collectors.toList());
        Map<String,List<Map<String,Object>>> groupByKqzqMc = closeMainData.stream().collect(Collectors.groupingBy(e->e.get("mc").toString()));
        if (groupByKqzqMc.size() > 0) {
            List<String> checkoutIdList = new ArrayList<>();
            groupByKqzqMc.forEach((k, v) -> {
                v.sort(Comparator.comparing((Map<String, Object> h) -> (String) h.get("nd"))
                        .thenComparing(h -> Integer.valueOf(h.get("yf").toString())));
                List<String> checkoutIdListItem = v.stream().map(e->e.get("id").toString()).collect(Collectors.toList());
                checkoutIdListItem.remove(checkoutIdListItem.size() - 1);
                if (checkoutIdListItem.size() > 0) {
                    checkoutIdList.addAll(checkoutIdListItem);
                }
            });
            if (checkoutIdList.size() > 0) {
                //更新考勤周期的状态为已结账
                sql = "update uf_jcl_kq_kqzq set zt = 2 where id in (" + String.join(",", checkoutIdList) + ")";
                boolean updateSign = DbTools.update(sql);
                log.info("AttendanceCycleCloseJob-考勤周期主表更新到已结账状态结果：" + updateSign);
            }
        }
        //处理考勤周期明细表
        sql = "select id, gzrq, gzsj, zt from uf_jcl_kq_kqzq_dt1 where zt != 1";
        List<Map<String, Object>> detailData = DbTools.getSqlToList(sql);
        detailData = detailData.stream().filter(f -> (Util.null2String(f.get("gzrq")) + " " + Util.null2String(f.get("gzsj"))).compareTo(currentTime) <= 0).collect(Collectors.toList());
        if (detailData.size() > 0) {
            List<String> ids = detailData.stream().map(e->e.get("id").toString()).collect(Collectors.toList());
            //更新考勤周期的状态为已关账
            sql = "update uf_jcl_kq_kqzq_dt1 set zt = 1 where id in (" + String.join(",", ids) + ")";
            boolean updateSign = DbTools.update(sql);
            log.info("AttendanceCycleCloseJob-考勤周期明细表更新状态结果：" + updateSign);
        }

        log.info("********AttendanceCycleCloseJob  end********");
    }
}
